﻿using System;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Portal.Repository.SkillsCatalog.Tests.System
{
	[TestClass()]
	public class CurriculumVitaeRepositoryTests : SkillsCatalogTests
	{
		private User _user = null;

		[TestCleanup]
		public override void TestCleanup()
		{
			base.TestCleanup();
			if (_user != null)
			{
				var user = PortalEntities.User.Remove(_user);
				PortalEntities.SaveChanges();
			}
		}

		#region GetByUser

		[TestMethod()]
		public void GetByUser_CvExists_ReturnCV()
		{
			InitializeUser();

			var entity = new CurriculumVitae()
			{
				UserId = _user.Id,
				UpdatedOn = DateTime.Now
			};
			Entities.CurriculumVitae.Add(entity);
			Entities.SaveChanges();

			var repository = GetRepository();
			var result = repository.GetByUser(_user.Id);

			result.Should().NotBeNull();
		}

		[TestMethod()]
		public void GetByUser_CvDoesNotExists_ReturnNull()
		{
			InitializeUser();

			var repository = GetRepository();
			var result = repository.GetByUser(_user.Id);

			result.Should().BeNull();
		}

		#endregion GetByUser

		private void InitializeUser()
		{
			_user = new User()
			{
				Login = Guid.NewGuid().ToString()
			};
			PortalEntities.User.Add(_user);
			PortalEntities.SaveChanges();
		}

		private ICurriculumVitaeRepository GetRepository()
		{
			return new CurriculumVitaeRepository(Entities);
		}
	}
}